<template>
  <div>

    <el-upload multiple style="display:inline-block" class="upload-demo" ref="upload" action="/hqx/knowledge/importKnowledge" :file-list="fileList" :http-request="uploadSectionFile" :auto-upload="false">
      <el-button slot="trigger" size="mini" type="primary" plain>选取文件</el-button>
      <el-button style="margin-left: 10px;" size="mini" icon="el-icon-upload2" type="success" @click="submitUpload">上传</el-button>
    </el-upload>
    <el-table :data="gridData" border style="width: 100%;margin-top:10px" size="mini">
      <el-table-column type="index" label="序号" align="center" width="50"></el-table-column>
      <el-table-column property="attaName" label="文件名"></el-table-column>
      <el-table-column property="fileType" label="文件类型" width="100"></el-table-column>
      <el-table-column property="createTime" label="上传时间" :formatter="createTimeFor" width="150"></el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button @click="toDown(scope.row)" type="text" size="small">下载</el-button>
          <el-button @click="handleDelete(scope.$index, scope.row)" type="text" size="small" style="color: #F56C6C;">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
export default {
  props: ["formData"],
  data() {
    return {
      ruleForm: this.formData,
      gridData: [],
      fileList: [],
      fjData: {
        typeDetailId: "", //附件业务明细id
        attaName: "", //附件名称
        busiKey: "", //业务关键字
        moduKey: "", //模块关键字
        attaHashCode: "", //附件HASH值
        fileName: "", //文件名
        sourFileName: "", //源代码文件名
        length: "", //长度
        fileType: "" //文件类型
      }
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    //获取文档数
    getUplaodNum() {
      return this.gridData.length;
    },

    createTimeFor(row, column) {
      return this.formatDate(row.createTime, "yyyy-MM-dd HH:mm:ss");
    },
    init() {
      let param = {
        busiKey: this.ruleForm.projId,
        typeDetailId: this.ruleForm.id
      };
      this.$api.formPost("/api/biz/baseAtta/doGetList", param).then(res => {
        if (res.retHead.code === "0000000") {
          this.gridData = res.retBody.list;
        }
      });
    },
    submitUpload() {
      let list = document.getElementsByClassName(
        "el-upload-list__item is-ready"
      );
      if (list.length == 0) {
        this.$message({
          type: "warning",
          message: "请选择需要上传的模板！"
        });
        return;
      }
      this.$refs.upload.submit();
    },
    //请求方法
    uploadSectionFile(param) {
      var fileObj = param.file;
      this.fjData.typeDetailId = this.formData.id;
      this.fjData.busiKey = this.formData.projId;
      this.fjData.moduKey = "";
      this.fjData.sourFileName = fileObj.name;
      let fdata = new FormData();
      // 文件对象
      fdata.append("file", fileObj);
      fdata.append("aliases", "");
      fdata.append("chunkNum", "1");
      fdata.append("wholeFileId", "");
      this.$api.formDataPost("/api/file/upload", fdata).then(res => {
        if (res.retHead.code == "0000000") {
          //保存附件到附件表
          this.fjData.attaName = res.retBody.fileName;
          this.fjData.fileName = res.retBody.fileName;
          this.fjData.fileType = res.retBody.fileType;
          this.fjData.length = res.retBody.fileSize;
          this.fjData.attaHashCode = res.retBody.fileHash;
          this.$api
            .formPost("/api/biz/baseAtta/doSave", this.fjData)
            .then(res => {
              if (res.retHead.code === "0000000") {
                this.fileList = [];

                let param = {
                  busiKey: this.formData.projId,
                  typeDetailId: this.formData.id
                };
                this.$api
                  .formPost("/api/biz/baseAtta/doGetList", param)
                  .then(res => {
                    if (res.retHead.code === "0000000") {
                      this.gridData = res.retBody.list;
                    }
                  });
              }
            });
        } else {
          this.$Message.error("上传失败，失败原因为" + res.retBody);
          return;
        }
      });
    },
    // //下载方法
    toDown(row) {
      window.location.href =
        this.baseURL +
        "/api/file/downloadFile/" +
        row.attaHashCode +
        "?Auth-User-Token=" +
        sessionStorage.getItem("token");
    },

    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$api
            .formPost("/api/biz/baseAtta/doDel", {
              id: row.id
            })
            .then(res => {
              if (res.retHead.code == "0000000") {
                this.$message({
                  message: "操作成功！",
                  type: "success"
                });
                this.init();
              } else {
                this.$message({
                  message: "操作失败！",
                  type: "error"
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
    // //删除方法
    // toDel(row) {
    //   this.$alert("这个操作会删除附件，是否删除？", "提示", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning",
    //     callback: action => {
    //       let param = {
    //         id: row.id
    //       };
    //       this.$api.formPost("/api/biz/baseAtta/doDel", param).then(res => {
    //         if (res.retHead.code === "0000000") {
    //           this.init();
    //           this.$message({
    //             type: "success",
    //             message: "删除成功！"
    //           });
    //         }
    //       });
    //     }
    //   });
    // }
  }
};
</script>
<style lang='scss' scoped>
</style>